#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable>

std::mutex mtx;
std::condition_variable cv;
int count = 0;
const int n = 10; // 缓冲区大小

void Tproduce() {
    std::unique_lock<std::mutex> lock(mtx);
    // 当缓冲区已满时等待
    cv.wait(lock, [] { return count < n; });
    
    std::cout << "(";
    count++;
    
    // 唤醒消费者
    cv.notify_one();
}

void Tconsume() {
    std::unique_lock<std::mutex> lock(mtx);
    // 当缓冲区为空时等待
    cv.wait(lock, [] { return count > 0; });
    
    std::cout << ")";
    count--;
    
    // 唤醒生产者
    cv.notify_one();
}

int main() {
    std::thread producers[50], consumers[50];
    
    // 创建生产者线程
    for (int i = 0; i < 50; i++) {
        producers[i] = std::thread(Tproduce);
    }
    
    // 创建消费者线程
    for (int i = 0; i < 50; i++) {
        consumers[i] = std::thread(Tconsume);
    }
    
    // 等待所有线程完成
    for (int i = 0; i < 50; i++) {
        producers[i].join();
        consumers[i].join();
    }
    
    return 0;
}
